import { NextResponse } from 'next/server';
import { getUserIdFromAuthHeader } from '@/lib/auth';
import { matchWish } from '@/lib/wishes';

export async function GET(request: Request) {
  try {
    const userId = getUserIdFromAuthHeader(request);
    if (!userId) {
      return NextResponse.json({ error: '未授权' }, { status: 401 });
    }
    const { searchParams } = new URL(request.url);
    const wishId = Number(searchParams.get('wishId'));
    if (!wishId || Number.isNaN(wishId)) {
      return NextResponse.json({ error: '缺少wishId' }, { status: 400 });
    }
    const data = await matchWish(userId, wishId);
    return NextResponse.json({ data });
  } catch (error) {
    console.error('匹配愿望错误:', error);
    return NextResponse.json({ error: '服务器错误' }, { status: 500 });
  }
}